<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="s" uri="/struts-tags"  %>
<!DOCTYPE html>
<html>
<head>
<jsp:include page="../inc.jsp"></jsp:include>
 <script type="text/javascript" charset="utf-8">
	var datagrid;
	var auth = "${auths}";
	var buttons = [];
	$(document).ready(function(){
	
		$('form input').bind('keyup', function(event) {// 增加回车提交功能 
			if (event.keyCode == '13') {
				datagrid.datagrid('load', sy.serializeObject($('#searchForm')));
			}
		});
		if(auth=="all"){
			buttons.push({text : '发起关联',iconCls : 'icon-add',handler : function() {append();}});
			buttons.push('-');
			buttons.push({text : '删除',iconCls : 'icon-remove',handler : function() {remove();}});
			buttons.push('-');
			buttons.push({text : '取消选中',iconCls : 'icon-undo',handler : function() {datagrid.datagrid('clearSelections');datagrid.datagrid('unselectAll');}});
			buttons.push('-');
		}else if(auth!=""){
			var auths = auth.split(",");
			if(auths.length>0){
				for(var i=0;i<auths.length;i++){
					if(auths[i]=="JXSGL_JXSCX_ADD"){
						buttons.push({text : '发起关联',iconCls : 'icon-add',handler : function() {append();}});
						buttons.push('-');
					}else if(auths[i]=="JXSGL_JXSCX_DELETE"){
						buttons.push({text : '删除',iconCls : 'icon-remove',handler : function() {remove();}});
						buttons.push('-');
					}
				}
				buttons.push({text : '取消选中',iconCls : 'icon-undo',handler : function() {datagrid.datagrid('clearSelections');datagrid.datagrid('unselectAll');}});
				buttons.push('-');
			}
		}
		
		datagrid = $('#datagrid').datagrid({
			url : 'dealerAction!datagrid.html',
			title : '经销商管理(标记为已授权经销商)',
			iconCls : 'icon-save',
			pagination : true,
			pagePosition : 'bottom',
			pageSize : 10,
			pageList : [ 10, 20, 30, 40 ],
			fit : true,
			fitColumns : false,
			nowrap : false,
			border : false,
			idField : 'id',
			sortName : 'id',
			sortOrder : 'desc',
			checkOnSelect : false,
			selectOnCheck : false,
			singleSelect : true,
			
			columns : [ [
			{
				title : '编号',
				field : 'id',
				width : 150,
				sortable : true,
				checkbox : true
			},{
				title : '经销商用户名',
				field : 'username',
				width : 80,
				sortable : true
			},{
				title : '联系人',
				field : 'realName',
				sortable : true,
				width : 70
			},{
				title : '手机',
				field : 'mobile',
				sortable : true,
				width : 80
			} ,{
				title : '地址',
				field : 'adress',
				sortable : true,
				width : 150
			} ,{
				title : '品牌Id',
				field : 'brandids',
				width : 150,
				hidden: true
			},{
				title : '品牌名称',
				field : 'brandname',
				width : 150,
				hidden: true
			} ,{
				title : '相关品牌',
				field : 'sameBrands',
				width : 150,
				hidden: false
			} ] ],
			toolbar : buttons,
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
		/* 
		//由于需求变动，不再显示已发起连接或者已建立连接的经销商，所以去掉这个样式更改
		$('#datagrid').datagrid({
			rowStyler:function(index,row){ 
				if (row.isGranted==true){   
					return 'background-color:#C7DEF0;'; 
				}  
			}
		}); */
		
	});
	function append() {
		var rows = datagrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
				parent.sy.messagerConfirm('请确认', '您确定要添加所选经销商？', function(r) {
				  if (r) {
					for ( var i = 0; i < rows.length; i++) {
						ids.push(rows[i].id);
					}
					$.ajax({
						url: 'dealerAction!add.html?ids='+ids,
						dataType: 'json',
						success: function(d){
								datagrid.datagrid('load');
								datagrid.datagrid('unselectAll');
								parent.sy.messagerShow({
									title : '提示',
									msg : d.msg
								});
						}
					});
			 	}
			});
		 }else {
		 	parent.sy.messagerAlert('提示', '请勾选要添加的记录！', 'error');
		 }
	}
	function remove() {
		var rows = datagrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			parent.sy.messagerConfirm('请确认', '您要删除当前所选项目？', function(r) {
				if (r) {
					for ( var i = 0; i < rows.length; i++) {
						ids.push(rows[i].id);
					}
					$.ajax({
						url : '${pageContext.request.contextPath}/jxsgl/dealerAction!delete.html',
						data : {
							ids : ids.join(',')
						},
						dataType : 'json',
						success : function(d) {
							datagrid.datagrid('load');
							datagrid.datagrid('unselectAll');
							parent.sy.messagerShow({
								title : '提示',
								msg : d.msg
							});
						}
					});
				}
			});
		} else {
			parent.sy.messagerAlert('提示', '请勾选要删除的记录！', 'error');
		}
	}
	function _search() {
		datagrid.datagrid('load', sy.serializeObject($('#searchForm')));
	}
	
	function cleanSearch() {
		datagrid.datagrid('load', {});
		$('#searchForm input').val('');
	}
 </script>
</head>
 
  <body class="easyui-layout" data-options="fit:true">
   	<div data-options="region:'north',border:false,title:'搜索条件'" style="height: 55px;overflow: hidden;" align="left">
		<form id="searchForm">
			<table class="tableForm datagrid-toolbar" style="width: 100%;height: 100%;">
				<tr>
					<th style="width:80px;">品牌名称</th>
					<td style="width:220px;"><input name="brandname" style="width:200px;" /></td>
					<th style="width:80px;">经销商姓名</th>
					<td style="width:220px;"><input name="realName" style="width: 200px;" /></td>
					<td><a href="javascript:void(0);" class="easyui-linkbutton" onclick="_search();">搜索</a><a href="javascript:void(0);" class="easyui-linkbutton" onclick="cleanSearch();">取消</a></td>
				</tr>
			</table>
		</form>
	</div>
	
	<div data-options="region:'center',border:false" style="overflow: hidden;">
		<table id="datagrid"></table>
	</div>
	<div id="menu" class="easyui-menu" style="width:120px;display: none;">
		<k:security key="QXGL_YHGL_ADD">
			<div onclick="append();" data-options="iconCls:'icon-add'">增加</div>
		</k:security>
		<k:security key="QXGL_YHGL_DELETE">
			<div onclick="remove();" data-options="iconCls:'icon-remove'">删除</div>
		</k:security>
		<k:security key="QXGL_YHGL_EDIT">
			<div onclick="edit();" data-options="iconCls:'icon-edit'">编辑</div>
		</k:security>
	</div>
<!--	<div style="display:block">-->
		
<!--	</div>-->
  </body>
</html>
